const mysql = require('mysql')
var bluebird = require('bluebird');

const pool = mysql.createPool({
  connectionLimit: 20, //连接池连接数
  host: '', //数据库地址，这里用的是本地
  database: '', //数据库名称
  user: '',  // username
  password: '' // password
})
let getPollConnection = bluebird.promisify(pool.getConnection, { context: pool });//将原getConnection回调函数变为promise函数
//将连接池的连接return出去
async function getUnTransactionConnection() {
  let result = await getPollConnection();
  return new Promise(function (resolve, reject) {
    resolve(result);
  })
}
async function getTransactionConnection() {
  let result = await getPollConnection();
  await result.beginTransaction()
  return new Promise(function (resolve, reject) {
    resolve(result);
  })
}

module.exports = {
  getUnTransactionConnection,
  getTransactionConnection
}